
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>URL编码/解码工具</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <style>
        :root {
            --primary: #6c5ce7;
            --secondary: #a29bfe;
            --accent: #fd79a8;
            --dark: #2d3436;
            --light: #f5f6fa;
        }
        
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
        }
        
        body {
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            min-height: 100vh;
            padding: 20px;
        }
        
        .container {
            max-width: 1000px;
            margin: 0 auto;
            background: white;
            border-radius: 15px;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
            overflow: hidden;
        }
        
        header {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            padding: 25px;
            text-align: center;
        }
        
        h1 {
            font-size: 2.2rem;
            margin-bottom: 10px;
        }
        
        .tab-container {
            display: flex;
            border-bottom: 1px solid #eee;
        }
        
        .tab {
            flex: 1;
            padding: 15px;
            text-align: center;
            cursor: pointer;
            font-weight: bold;
            color: var(--dark);
            transition: all 0.3s ease;
        }
        
        .tab.active {
            color: var(--primary);
            border-bottom: 3px solid var(--primary);
        }
        
        .tab-content {
            padding: 30px;
            display: none;
        }
        
        .tab-content.active {
            display: block;
        }
        
        .input-group {
            margin-bottom: 20px;
        }
        
        label {
            display: block;
            margin-bottom: 8px;
            font-weight: bold;
            color: var(--dark);
        }
        
        input, textarea {
            width: 100%;
            padding: 12px;
            border: 2px solid #eee;
            border-radius: 8px;
            font-size: 16px;
            transition: all 0.3s ease;
        }
        
        textarea {
            min-height: 120px;
            resize: vertical;
        }
        
        input:focus, textarea:focus {
            border-color: var(--secondary);
            outline: none;
        }
        
        .button-group {
            display: flex;
            gap: 15px;
            margin-bottom: 20px;
        }
        
        button {
            padding: 12px 25px;
            border: none;
            border-radius: 8px;
            background: var(--primary);
            color: white;
            cursor: pointer;
            font-weight: bold;
            font-size: 16px;
            transition: all 0.3s ease;
            flex: 1;
        }
        
        button:hover {
            background: var(--secondary);
            transform: translateY(-2px);
        }
        
        button:active {
            transform: translateY(0);
        }
        
        .result-box {
            margin-top: 20px;
            padding: 20px;
            background: #f8f9fa;
            border-radius: 8px;
            border-left: 4px solid var(--primary);
        }
        
        .history {
            margin-top: 30px;
        }
        
        .history-title {
            font-size: 1.2rem;
            margin-bottom: 15px;
            padding-bottom: 10px;
            border-bottom: 1px solid #eee;
        }
        
        .history-list {
            max-height: 200px;
            overflow-y: auto;
        }
        
        .history-item {
            padding: 10px;
            margin-bottom: 8px;
            background: rgba(162, 155, 254, 0.1);
            border-radius: 6px;
            cursor: pointer;
            transition: all 0.2s ease;
        }
        
        .history-item:hover {
            background: rgba(162, 155, 254, 0.2);
        }
        
        .history-item .time {
            font-size: 0.8rem;
            color: #666;
            margin-top: 5px;
        }
        
        @media (max-width: 768px) {
            .container {
                border-radius: 0;
            }
            
            header {
                padding: 20px 15px;
            }
            
            h1 {
                font-size: 1.8rem;
            }
            
            .tab-content {
                padding: 20px 15px;
            }
            
            .button-group {
                flex-direction: column;
            }
        }
    </style>
</head>
<body>
    <div class="container">
        <header>
            <h1><i class="fas fa-link"></i> URL编码/解码工具</h1>
            <p>快速处理URL编码与解码，解决特殊字符问题</p>
        </header>
        
        <div class="tab-container">
            <div class="tab active" data-tab="encode">URL编码</div>
            <div class="tab" data-tab="decode">URL解码</div>
        </div>
        
        <div class="tab-content active" id="encode">
            <div class="input-group">
                <label for="encode-input">原始文本/URL</label>
                <textarea id="encode-input" placeholder="输入需要编码的文本或URL..."></textarea>
            </div>
            
            <div class="button-group">
                <button id="encode-btn"><i class="fas fa-lock"></i> 编码</button>
                <button id="copy-encode"><i class="fas fa-copy"></i> 复制结果</button>
                <button id="clear-encode"><i class="fas fa-trash"></i> 清空</button>
            </div>
            
            <div class="result-box">
                <label for="encode-output">编码结果</label>
                <textarea id="encode-output" readonly></textarea>
            </div>
            
            <div class="history">
                <div class="history-title">编码历史</div>
                <div class="history-list" id="encode-history"></div>
            </div>
        </div>
        
        <div class="tab-content" id="decode">
            <div class="input-group">
                <label for="decode-input">编码后的URL</label>
                <textarea id="decode-input" placeholder="输入需要解码的URL编码字符串..."></textarea>
            </div>
            
            <div class="button-group">
                <button id="decode-btn"><i class="fas fa-unlock"></i> 解码</button>
                <button id="copy-decode"><i class="fas fa-copy"></i> 复制结果</button>
                <button id="clear-decode"><i class="fas fa-trash"></i> 清空</button>
            </div>
            
            <div class="result-box">
                <label for="decode-output">解码结果</label>
                <textarea id="decode-output" readonly></textarea>
            </div>
            
            <div class="history">
                <div class="history-title">解码历史</div>
                <div class="history-list" id="decode-history"></div>
            </div>
        </div>
    </div>

    <script>
        document.addEventListener('DOMContentLoaded', function() {
            // 切换标签页
            const tabs = document.querySelectorAll('.tab');
            tabs.forEach(tab => {
                tab.addEventListener('click', function() {
                    tabs.forEach(t => t.classList.remove('active'));
                    this.classList.add('active');
                    
                    document.querySelectorAll('.tab-content').forEach(content => {
                        content.classList.remove('active');
                    });
                    
                    document.getElementById(this.dataset.tab).classList.add('active');
                });
            });
            
            // 初始化历史记录
            let encodeHistory = JSON.parse(localStorage.getItem('encodeHistory')) || [];
            let decodeHistory = JSON.parse(localStorage.getItem('decodeHistory')) || [];
            
            // 更新历史记录显示
            function updateHistory() {
                const encodeHistoryEl = document.getElementById('encode-history');
                const decodeHistoryEl = document.getElementById('decode-history');
                
                encodeHistoryEl.innerHTML = '';
                decodeHistoryEl.innerHTML = '';
                
                encodeHistory.slice(0, 10).forEach(item => {
                    const historyItem = document.createElement('div');
                    historyItem.className = 'history-item';
                    historyItem.innerHTML = `
                        <div>${item.original.substring(0, 50)}${item.original.length > 50 ? '...' : ''}</div>
                        <div class="time">${new Date(item.time).toLocaleString()}</div>
                    `;
                    historyItem.addEventListener('click', () => {
                        document.getElementById('encode-input').value = item.original;
                        document.getElementById('encode-output').value = item.encoded;
                    });
                    encodeHistoryEl.appendChild(historyItem);
                });
                
                decodeHistory.slice(0, 10).forEach(item => {
                    const historyItem = document.createElement('div');
                    historyItem.className = 'history-item';
                    historyItem.innerHTML = `
                        <div>${item.encoded.substring(0, 50)}${item.encoded.length > 50 ? '...' : ''}</div>
                        <div class="time">${new Date(item.time).toLocaleString()}</div>
                    `;
                    historyItem.addEventListener('click', () => {
                        document.getElementById('decode-input').value = item.encoded;
                        document.getElementById('decode-output').value = item.decoded;
                    });
                    decodeHistoryEl.appendChild(historyItem);
                });
            }
            
            updateHistory();
            
            // URL编码
            document.getElementById('encode-btn').addEventListener('click', function() {
                const input = document.getElementById('encode-input').value.trim();
                if (!input) {
                    alert('请输入要编码的内容');
                    return;
                }
                
                const encoded = encodeURIComponent(input);
                document.getElementById('encode-output').value = encoded;
                
                // 保存到历史记录
                encodeHistory.unshift({
                    original: input,
                    encoded: encoded,
                    time: new Date().toISOString()
                });
                
                if (encodeHistory.length > 20) {
                    encodeHistory.pop();
                }
                
                localStorage.setItem('encodeHistory', JSON.stringify(encodeHistory));
                updateHistory();
            });
            
            // URL解码
            document.getElementById('decode-btn').addEventListener('click', function() {
                const input = document.getElementById('decode-input').value.trim();
                if (!input) {
                    alert('请输入要解码的内容');
                    return;
                }
                
                try {
                    const decoded = decodeURIComponent(input);
                    document.getElementById('decode-output').value = decoded;
                    
                    // 保存到历史记录
                    decodeHistory.unshift({
                        encoded: input,
                        decoded: decoded,
                        time: new Date().toISOString()
                    });
                    
                    if (decodeHistory.length > 20) {
                        decodeHistory.pop();
                    }
                    
                    localStorage.setItem('decodeHistory', JSON.stringify(decodeHistory));
                    updateHistory();
                } catch (e) {
                    alert('解码失败，请检查输入是否为有效的URL编码字符串');
                }
            });
            
            // 复制功能
            document.getElementById('copy-encode').addEventListener('click', function() {
                const output = document.getElementById('encode-output');
                output.select();
                document.execCommand('copy');
                alert('已复制到剪贴板');
            });
            
            document.getElementById('copy-decode').addEventListener('click', function() {
                const output = document.getElementById('decode-output');
                output.select();
                document.execCommand('copy');
                alert('已复制到剪贴板');
            });
            
            // 清空功能
            document.getElementById('clear-encode').addEventListener('click', function() {
                document.getElementById('encode-input').value = '';
                document.getElementById('encode-output').value = '';
            });
            
            document.getElementById('clear-decode').addEventListener('click', function() {
                document.getElementById('decode-input').value = '';
                document.getElementById('decode-output').value = '';
            });
            
            // 键盘快捷键
            document.addEventListener('keydown', function(e) {
                if (e.ctrlKey && e.key === 'Enter') {
                    const activeTab = document.querySelector('.tab.active');
                    if (activeTab.dataset.tab === 'encode') {
                        document.getElementById('encode-btn').click();
                    } else {
                        document.getElementById('decode-btn').click();
                    }
                }
            });
        });
    </script>
</body>
</html>
